package core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * @author Arno de Jager
 *
 */
@SuppressWarnings("serial")
public class Launcher extends JApplet implements ActionListener {

	private ArrayList<Map> maps = new ArrayList<Map>();
	
	public void init() {
		generateMaps();
		setSize( 990, 485 );
		
		setContentPane( preView() );
	}
	
	/**
	 * Vult de array met maps
	 */
	private void generateMaps() {
		ArrayList<ArrayList<Point2D.Double>> routes = new ArrayList<ArrayList<Point2D.Double>>();
		
		//Route 1
		ArrayList<Point2D.Double> route1 = new ArrayList<Point2D.Double>();
		route1.add( new Point2D.Double( 0, 250 ) );
		route1.add( new Point2D.Double( 550, 250 ) );
		routes.add(route1);
		maps.add ( new Map( new Base( new Point2D.Double( 550, 230) ), routes, 1 ) );
		//Route 1
		
		//Route 2
		routes = new ArrayList<ArrayList<Point2D.Double>>();
		
		ArrayList<Point2D.Double> route2 = new ArrayList<Point2D.Double>();
		route2.add( new Point2D.Double( 0, 100 ) );
		route2.add( new Point2D.Double( 550, 100 ) );
		route2.add( new Point2D.Double( 550, 200 ) );
		route2.add( new Point2D.Double( 100, 200 ) );
		route2.add( new Point2D.Double( 50, 230 ) );
		
		ArrayList<Point2D.Double> route3 = new ArrayList<Point2D.Double>();
		route3.add( new Point2D.Double( 0, 360 ) );
		route3.add( new Point2D.Double( 550, 360 ) );
		route3.add( new Point2D.Double( 550, 260 ) );
		route3.add( new Point2D.Double( 100, 260 ) );
		route3.add( new Point2D.Double( 50, 230 ) );
		
		routes.add(route2);
		routes.add(route3);
		maps.add ( new Map( new Base( new Point2D.Double( 50, 215) ), routes, 2 ) );
		//Route 2
		
		//Route 3
		//Volg bovenstaande voorbeelden voor verdere routes
		//Route 3
	}
	
	/**
	 * @param i nummer van de map (Geen index)
	 */
	private void setMap(int i) {
		i--;
		Model m = new Model( maps.get(i) );
		
		Controller c = new Controller( m );
		
		setContentPane(c.getView());
	}
	
	/**
	 * @return Het paneel met de knoppen van de mappen die je kan kiezen
	 */
	private JPanel preView() {
		JPanel panel = new JPanel();
		
		for(int i = 1; i <= maps.size(); i++) {
			JButton j = new JButton(new ImageIcon("images/map" + i + ".gif"));
			j.addActionListener(this);
			j.setActionCommand("" + i);
			panel.add(j);
		}
		
		return panel;
	}
	
	/**
	 * De Actionlistener voor dit gedeelte
	 */
	public void actionPerformed(ActionEvent evt) {
		setMap( Integer.parseInt( evt.getActionCommand() ) );
	}
	
}
